<!-- 订单类型定义：蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="mt10 flex">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }" style="width: 100%">
        <div class="flex">
          <h1 class="f16">订单类型定义</h1>
          <JDialog :tableHeader="JTableHeader" :width="73" :currentTableRow="currentTableRow" @confirm="confirm" :unEdit="false" :unSub="true" />
        </div>
        <Table
          :tableHeader="tableHeader"
          @selectList="selectTableList"
          :tableData="tableData"
          selection
          class="mt10"
          :isLoading="isLoading"
          @currentRow="currentRow"
        />
        <el-card shadow="never" :body-style="{ padding: '5px' }">
          <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
        </el-card>
      </el-card>
    </div>
  </div>
</template>

<script lang="ts" setup name="OrderType">
import JMessage from '@/utils/JMessage'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { watch } from 'vue'
import { onMounted } from 'vue'
import { ref } from 'vue'

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入类型名称',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '查询'
  },
  {
    type: 'button',
    checkboxLabel: '删除打勾数据'
  }
])

// 表一模块
// 表一按钮
const findClicked = (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '查询') {
    getTableList()
  }
  if (content === '删除打勾数据') {
    removeList()
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
const tableHeader = ref([
  {
    label: '订单类型',
    prop: 'orderType'
  },
  {
    label: '类型名称',
    prop: 'typeName'
  },
  {
    label: '备注',
    prop: 'remark'
  }
])
const JTableHeader = ref([
  {
    type: 'selectJV2',
    label: '是否影响库存',
    prop: 'invFlag',
    options: [
      {
        label: '是',
        value: '1'
      },
      {
        label: '否',
        value: '0'
      }
    ]
  },
  {
    type: 'selectJV2',
    label: '订单类型',
    prop: 'orderType',
    options: [
      {
        label: '入库',
        value: '1'
      },
      {
        label: '出库',
        value: '0'
      }
    ]
  },
  {
    label: '类型名称',
    prop: 'typeName'
  },
  {
    label: '备注',
    prop: 'remark'
  }
])
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>(`/store/OrderType/list?search=${formList.value[0].inputValue}`, {
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.data.records.map((item: any) => {
    if (item.orderType === '1') item.orderType = '入库'
    if (item.orderType === '0') item.orderType = '出库'
    return item
  })
  total.value = tableDataList.data.total
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0]) => {
  currentTableRow.value = row
}
const selectedLists = ref([]) as any
const selectTableList = (selectedList: any) => {
  selectedLists.value = selectedList
}
// Dialog确认添加/修改按钮
const confirm = async (obj: any, dialogVisible: any, title: any) => {
  // 向服务器发送请求，更新外派单信息
  if (title === '添加') {
    const res = (await service.put(`/store/OrderType/add`, [obj])) as any
    // 判断请求是否成功
    if (res.code === 200) {
      JMessage.success('添加成功')
      // 关闭弹出框
      dialogVisible.value = false
      // 重新获取表格数据
      getTableList()
    } else {
      JMessage.error(res.msg)
    }
  } else if (title === '修改') {
    const res = (await service.put(`/store/OrderType/update`, [obj])) as any
    // 判断请求是否成功
    if (res.code === 200) {
      JMessage.success('修改成功')
      // 关闭弹出框
      dialogVisible.value = false
      // 重新获取表格数据
      getTableList()
    } else {
      JMessage.error(res.msg)
    }
  }
}
// 删除
const removeList = async () => {
  if (selectedLists.value.length === 0) {
    JMessage.error('请勾选表格中的数据')
    return
  }
  // 向服务器发送请求，更新外派单信息
  const res = (await service.post(
    `/store/OrderType/delete`,
    selectedLists.value.map((item: any) => item.id)
  )) as any
  // 判断请求是否成功
  if (res.code === 200) {
    JMessage.success('删除成功')
    // 重新获取表格数据
    getTableList()
  } else {
    JMessage.error(res.msg)
  }
}

// 表二模块
const tableData2 = ref([])
const isLoading2 = ref(true)
const tableHeader2 = ref([
  {
    label: '仓库名称',
    prop: 'warehouseName'
  },
  {
    label: '仓库类型',
    prop: 'warehouseType'
  },
  {
    label: '仓库地址',
    prop: 'address'
  },
  {
    label: '备注',
    prop: 'remark'
  }
])
const total2 = ref(0)
const currentPage2 = ref(1)
const pageSize2 = ref(40)
const handleSizeChange2 = (val: number) => {
  pageSize2.value = val
  getTableList2()
}
const handleCurrentChange2 = (val: number) => {
  currentPage2.value = val
  getTableList2()
}

const getTableList2 = debounce(async () => {
  isLoading2.value = true
  let tableDataList2 = await service.post<any, any>(`/store/warehouse/query?id=${currentTableRow.value.detailId}`)
  isLoading2.value = false
  tableData2.value = tableDataList2.data.records
  total2.value = tableDataList2.data.total
  currentRow2()
}, 500)
const currentTableRow2 = ref({}) as any
const currentRow2 = async (row: any = tableData2.value[0]) => {
  currentTableRow2.value = row
}
const selectedLists2 = ref([]) as any
const selectTableList2 = (selectedList: any) => {
  selectedLists2.value = selectedList
}
const confirm2 = async (obj: any, dialogVisible: any, title: any) => {
  // 向服务器发送请求，更新外派单信息
  if (title === '添加') {
    const res = (await service.put(`/store/warehouse/save`, {
      ...currentTableRow.value,
      address: [obj]
    })) as any
    // 判断请求是否成功
    if (res.code === 200) {
      JMessage.success(res.msg)
    } else {
      JMessage.error(res.msg)
    }
  } else if (title === '修改') {
    const res = (await service.put(`/store/warehouse/save`, {
      ...currentTableRow.value,
      address: [obj]
    })) as any
    // 判断请求是否成功
    if (res.code === 200) {
      JMessage.success(res.msg)
    } else {
      JMessage.error(res.msg)
    }
  }
  // 关闭弹出框
  dialogVisible.value = false
  // 重新获取表格数据
  getTableList()
}
</script>
